/* eslint-disable no-unused-vars */
/* eslint-disable react/destructuring-assignment */
// 供应商列表
import React from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';
import HOCSelect from '@/components/AntDesign/Select';

const { Option } = Select;

@connect(({ publicdata: { supplyList, supplyFsValue }, loading }) => ({
  supplyList,
  supplyFsValue,
  loading: loading.effects['publicdata/fetchSupplyList'],
}))
class Supplier extends React.Component {
  componentDidMount() {
    this.props.dispatch({
      type: 'publicdata/fetchSupplyList',
      payload: { name: null, ...this.props.params },
    });
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'publicdata/updateModelData',
      payload: { supplyList: [] },
    });
  }


  getData = (value, options) => {};

  // 更改当前选择的值并供给父组件用
  triggerChange(changeValue, options) {
    const { onChange, dispatch, ...rest } = this.props;
    if (onChange) {
      onChange(changeValue, options);
    }
    dispatch({
      type: 'publicdata/updateModelData',
      payload: {
        supplyFsValue: rest.labelInValue ? { key: options.key, label: options.title } : options,
      },
    });
  }

  render() {
    const { supplyList = [], loading, getData = this.getData, value, supplyFsValue,  ...rest } = this.props;
    return (
      <HOCSelect
        placeholder="请输入关键字检索"
        {...rest}
        showSearch
        value={value ? (rest.labelInValue && supplyFsValue) || supplyFsValue.title : value}
        optionFilterProp="children"
        onSelect={(selectValue, options) => getData(selectValue, options)}
        notFoundContent={loading ? <Spin size="small" /> : '暂无数据'}
        onChange={(changeValue, options) => this.triggerChange(changeValue, options)}
      >
        {supplyList.map(item => (
          <Option key={item.suppId} title={item.name} fsOptions={item}>
            {item.name}
          </Option>
        ))}
      </HOCSelect>
    );
  }
}
export default Supplier;
